package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.FeatureTraining;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.vo.feature.FeatureResponse;
import com.example.teesystem.entity.vo.feature.FeatureTrainingRequestVo;
import com.example.teesystem.entityDto.IssueBriefMessage;
import com.example.teesystem.entityDto.IssueMessage;
import com.example.teesystem.mapper.IssueMapper;
import com.example.teesystem.service.FeatureTrainingService;
import com.example.teesystem.mapper.FeatureTrainingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_feature_training】的数据库操作Service实现
* @createDate 2023-01-14 15:32:50
*/
@Service
@RequiredArgsConstructor
public class FeatureTrainingServiceImpl extends ServiceImpl<FeatureTrainingMapper, FeatureTraining>
    implements FeatureTrainingService{

    private final IssueMapper issueMapper;
    private final FeatureTrainingMapper featureTrainingMapper;

    @Override
    public Response getIssue(FeatureTrainingRequestVo featureTrainingRequestVo) {
        Page<FeatureTraining> page = new Page<>(featureTrainingRequestVo.getCur(), featureTrainingRequestVo.getSize());
        QueryWrapper<FeatureTraining> wrapper = new QueryWrapper<>();

        wrapper.eq("is_delete",0)
                .eq("course_id",featureTrainingRequestVo.getCourseId())
                .orderByDesc("upload_time");
        Page<FeatureTraining> myPage = this.page(page, wrapper);

        List<FeatureTraining> featureTrainingIssueList = myPage.getRecords();
        List<IssueBriefMessage> issueBriefMessages = new ArrayList<>();

        if(featureTrainingIssueList.size() == 0 ){
            return Response.ok(issueBriefMessages);
        }

        List<Integer> integerList = new ArrayList<>();
        for (FeatureTraining featureTraining : featureTrainingIssueList) {
            integerList.add(featureTraining.getIssueId());
        }
        List<Issue> issues = issueMapper.getIssuesByIds(integerList);

        for(Issue issue : issues){
            issueBriefMessages.add(new IssueBriefMessage(issue));
        }
        return Response.ok(issueBriefMessages);
    }

    @Override
    public Response teacherGetIssue(FeatureTrainingRequestVo featureTrainingRequestVo) {
        Page<FeatureTraining> page = new Page<>(featureTrainingRequestVo.getCur(), featureTrainingRequestVo.getSize());
        QueryWrapper<FeatureTraining> wrapper = new QueryWrapper<>();

        wrapper.eq("is_delete",0)
                .eq("course_id",featureTrainingRequestVo.getCourseId())
                .orderByDesc("upload_time");
        Page<FeatureTraining> myPage = this.page(page, wrapper);

        List<FeatureTraining> featureTrainingIssueList = myPage.getRecords();
        List<FeatureResponse> featureResponses = new ArrayList<>();

        if(featureTrainingIssueList.size() == 0 ){
            return Response.ok(featureResponses);
        }

        for (FeatureTraining featureTraining : featureTrainingIssueList) {
            Issue issue = issueMapper.selectById(featureTraining.getIssueId());
            featureResponses.add(new FeatureResponse(featureTraining,new IssueMessage(issue)));
        }
        return Response.ok(featureResponses);
    }

    @Override
    public Response delete(int id) {
        try {
            featureTrainingMapper.delete(id);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.DELETE_INFO_FAIL);
        }
    }

    @Override
    public Response add(int issueId) {
        Issue issue = issueMapper.selectById(issueId);
        FeatureTraining featureTraining = new FeatureTraining();
        featureTraining.setIssueId(issue.getId());
        featureTraining.setCourseId(issue.getCourseId());
        featureTrainingMapper.insert(featureTraining);
        return Response.ok();
    }
}




